<?php

class Upload
{

	static function getFileExtension($name)
	{
		$last = strrpos($name, '.');
		$ext = substr($name, $last + 1);
		return $ext;
	}

	static function createTemporaryPath($name)
	{
		return sha1(microtime(1, 0), false) . '.' . self::getFileExtension($name);
	}

	function makeTemporaryFile($name)
	{
		if (!isset($_FILES[$name]))
		{
			return null;
		}

		$file = $_FILES[$name];

		if ($file['error'])
		{
			return null;
		}

		if (empty($file['name']))
		{
			return null;
		}

		if ($file['size'] == 0)
		{
			return null;
		}

		if ($file['tmp_name'] == '')
		{
			return null;
		}

		$filename = $file['name'];

		$temp = $file['tmp_name'];

		$temp2 = self::createTemporaryPath($filename);

		$destination = AGILET_TEMPORARY_PATH . '/file/' . $temp2;

		if (move_uploaded_file($temp, $destination))
		{
			@chmod($destination, 0777);
			return Agilet::getUrl('/temporary/file/' . $temp2);
		}

	}

}
